<?php

ini_set ( "display_errors", 1 );
error_reporting ( E_ALL );

/*
 * REST URLS and ATTRIBUTES [POST]
 * 
 * http://89.117.25.29:8080/~thespy/pervasive/rest.php?action=[ACTION]
 * 
 * upload_trips
 * var: json
 * {"trip_id":"id", "trip_name":"test_name","path_points":[[p_lat,p_lng],[p_lat,p_lng],[p_lat,p_lng]],"pin_points":[[p_lat,p_long,"note_text"]]}
 * 
 * delete_trip
 * var: trip_id
 * 
 * add_point
 * var: trip_id, longitude, latitude, type[enum("pin","path")]
 * 
 * delete_point
 * var: point_id
 * 
 * add_note
 * var: point_id, text
 * 
 * delete_note
 * var: note_id
 * 
 * [
 * {"trip_id":2,"path_points":[[12.52002374,55.70018725999999]],"trip_name":"fhhd","pin_points":[]}
 * ,{"trip_id":1,"path_points":[[12.520268000000002,55.69801866666666],[12.520097680000001,55.70011956],[12.5200338,55.700178375],[12.520238075000002,55.700134425],[12.52002374,55.70018725999999],[12.5200338,55.700178375],[12.5200338,55.700178375],[12.520195175,55.700141562499994],[12.520052275000001,55.700184875000005],[12.520195175,55.700141562499994],[12.520171228571428,55.70012022857143]],"trip_name":"ff","pin_points":[]}
 * ]
 */

require_once 'service.php';

if (isset ( $_GET ["action"] ) && ! empty ( $_GET ["action"] )) {
	
	switch ($_GET ["action"]) {
		
		case "upload_trip" :
			if (isset ( $_POST ["json"] ) && ! empty ( $_POST ["json"] )) {
				
				$data_list = json_decode ( $_POST ["json"] );
				
				$retVal = array ();
				
				foreach ( $data_list as $data ) {
					
					$trip_remote_id = addTrip ( $data->trip_name );
					$trip_local_id = $data->trip_id;
					
					if (! empty ( $data->path_points ))
						foreach ( $data->path_points as $k => $v )
							addPoint ( $trip_remote_id, $v [0], $v [1], "path" );
					
					if (! empty ( $data->pin_points ))
						foreach ( $data->pin_points as $k => $v ) {
							$pointId = addPoint ( $trip_remote_id, $v [0], $v [1], "pin" );
							addNote ( $pointId, $v [2] );
						}
					
					$retVal [] = array ("trip_local_id" => $trip_local_id, "trip_remote_id" => $trip_remote_id );
				}
				
				echo json_encode ( array ("error" => "0", "message" => "Trip successfully uploaded", "mappings" => $retVal, "action" => $_GET ["action"] ) );
			} else {
				echo json_encode ( array ("error" => "1", "message" => "JSON string is not defined or empty", "action" => $_GET ["action"] ) );
			}
			break;
		
		case "get_trips" :
			
			$trips = getTrips ();
			echo json_encode ( array ("error" => "0", "message" => "Trips successfully downloaded", "trips" => $trips, "action" => $_GET ["action"] ) );
			
			break;
		
		case "get_trip_data" :
			
			if (isset ( $_POST ["trip_id"] ) && ! empty ( $_POST ["trip_id"] )) {
				
				$trip = getTrip ( $_POST ["trip_id"] );
				
				if ($trip != null) {
					
					$path_points = getPoints ( $_POST ["trip_id"], "path" );
					$pin_points = getPoints ( $_POST ["trip_id"], "pin" );
					
					$pin_points = attachNotes ( $pin_points );
					
					echo json_encode ( array ("error" => "0", "message" => "Trip successfully downloaded", "trip_id" => $_POST ["trip_id"], "trip_name" => $trip["name"], "path_points" => $path_points, "pin_points" => $pin_points, "action" => $_GET ["action"] ) );
					
				} else {
					echo json_encode ( array ("error" => "1", "message" => "trip with id ". $_POST["trip_id"]." does not exist." , "action" => $_GET ["action"] ) );
				}
			} else {
				echo json_encode ( array ("error" => "1", "message" => "trip_id is not defined", "action" => $_GET ["action"] ) );
			}
			
			break;
	
	}

}

?>